package com.shangqing.system.service.impl;

import com.shangqing.system.domain.caoyuan.vo.CityVo;
import com.shangqing.system.domain.rainfall.Rainfall;
import com.shangqing.system.mapper.CityMapper;
import com.shangqing.system.mapper.EarlyMapper;
import com.shangqing.system.mapper.GradeMapper;
import com.shangqing.system.mapper.RainfallMapper;
import com.shangqing.system.service.RainfallService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RainfallServiceImpi implements RainfallService {
    @Autowired
    private RainfallMapper rainfallMapper;
    @Autowired
    private GradeMapper gradeMapper;
    @Autowired
    private EarlyMapper earlyMapper;
    @Autowired
    private CityMapper cityMapper;

    /**
     * 获取灾情信息
     *
     * @return
     */
    @Override
    public List<Rainfall> selectall() {
        List<Rainfall> list = new ArrayList<>(); //定义返回对象
        List<Rainfall> rainfallList = rainfallMapper.selectall(); //雨量等级
        for (Rainfall rainfall : rainfallList
        ) {
            rainfall.setGrade(gradeMapper.selectById(rainfall.getGradeId())); // 查询
            CityVo city = cityMapper.select(rainfall.getCityId());//获取当前地址
            city.setCities(cityVos(city));// 递归获取地址
            rainfall.setCities(city);
            rainfall.setEarly(earlyMapper.selectById(rainfall.getEarlyId())); // 查询预警信息
            list.add(rainfall);
        }
        return list;
    }

    private CityVo cityVos(CityVo list) {
        if (list.getParentId() == 0) {
            return null;      //递归出口
        } else {
            CityVo vo = cityMapper.select(list.getParentId());//获取父ID对应的地址
            CityVo cityVo = new CityVo();//封装
            cityVo.setId(vo.getId());
            cityVo.setCityName(vo.getCityName());
            cityVo.setParentId(vo.getParentId());
            cityVo.setCities(cityVos(vo)); //进行递归进口
            return cityVo;
        }
    }
}
